Git is a version control system. Version control helps you to track the changes you have made in your project, so you can go back to an earlier stage if needed and it is a good, easily trackable way to collaborate with others.
A project or repository is basically a folder where you have all your codes, files, subfolders, etc on your local computer. You can upload this repository to a remote server (GitHub, GitLab, etc), so you won’t depend only on your computer.
Also, this way others can collaborate to your project or you can download a project made by someone else and collaborate to it.
What Git does is it tracks the files in your project. You can create a snapshot of it any time and upload it to a remote server.
First you need to download Git. You can find help on the RStudio’s website on how to install Git on your computer.
This project is on GitHub, so you will need to register there to be able to download the project and make modifications in it.
You can download an existing project from a remote server, eg.: GitHub. You can do this via RStudio:
File > New Project > Version Control > GitClone or download and then copy the address you see there (make sure you copy the http address, not ssh).You can check your current status, which shows the changes you have made since the last save (or commit in Git terms - we will get to this soon).
Go to the Git panel in RStudio
Every new file to the project or the changes you have made in a file is in the ‘unstaged’ area, which means it is not tracked by Git yet. You can add a file (or the changes in it) by ticking the box next to the file. This way the file will move to the staging area:
Once you have started to make changes, you can save it at any point. This way you can go back to this point any time later. You can commit (= save) any file which is in the staging area.
Commit button, a new window will pop up. Here you will see the changes you have made since your last commit: red shows the rows you have removed and green shows new rows (if you change eg. a few letters in a row, Git will show you that you have deleted that row and creted a new one.) If you have added a brand new file, you will see it in all green.After commiting your files you can press the Push button on the upper right corner.
Since others are also able to push in the repo, next time before you do anything, you should press the Pull button.
You can do the same things in command line. There is no difference if you do it in RStudio or in command line, is just personal preference wh. You need to have a Command Line Interface (such as Terminal for Mac or Command Prompt for Windows)
git clone remote-repository-url
git status
git diff (this shows all changes in all files)
git diff selected_file (this shows changes you have made in a selected file)
You can select a few files to add git add write_down all_selected files_here
Or you can select all files git add .
git commit -m "write your commit message here"
git push
git pull
The .gitignore file: You can write anything into the .gitignore file, which you don’t want to track and don’t want to push it in the remote repository. We do this usually to files which contains some kind of secret (eg. password) or to huge files/images. Note that this is a hidden file, which can be seen in RStudio’s file manager, but on most file managers you need enable showing hidden files to be able to see .gitignore.
Checking your history with git log: In RStudio you can get the logs by pressing on History. This command shows the commit history of the project. You can see the commit messages which can give you an idea, what happened in the project. You can also use the SHA key (8 characters, mixture of numbers and letters), to go back to a certain state in your project.
Go back to an earlier state with git checkout SHA-key: This is not possible in RStudio, only with command line. By providing the SHA-key you can go back to any saved state in your project. You can go back to your current state by typing git checkout master
Some more advanced stuff (not necessary to know now, but can be very useful in the future)